package mo.offer_version2;

import java.util.Stack;

public class L31 {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed.length == 0 && popped.length == 0) {
            return true;
        }
        Stack<Integer> stack = new Stack<>();
        int popIndex = 0;
        for (int num : pushed) {
            stack.push(num);
            while (!stack.isEmpty() && popIndex < popped.length && stack.peek() == popped[popIndex]) {
                stack.pop();
                popIndex++;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        L31 test = new L31();
        System.out.println(test.validateStackSequences(new int[]{1,2,3,4,5}, new int[]{4,5,3,2,1}));
    }
}
